﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_Ly_Phong_Kham_co_len_em
{
    public partial class frmtrangthai : Form
    {
        public frmtrangthai()
        {
            InitializeComponent();

            TV_Tonghop.Themcot("MA_TRANG_THAI", "Mã Trạng Thái", dt_trangthai, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("TEN_TRANG_THAI", "Tên Trạng Thái", dt_trangthai, DataGridViewAutoSizeColumnMode.Fill);

        }

        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=NGUYENTY-PC\\SQLEXPRESS;Initial Catalog=QL_PHONGKHAM;Integrated Security=True");

        }

        private void frmtrangthai_Load(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            SqlDataAdapter ad = new SqlDataAdapter("HienTrangthai", ketnoi); //ket noi den co so du lieu de goi ham da khai bao trong StoredProcedure
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dt_trangthai.DataSource = dt;
        }

        private void bt_Them_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("ThemTrangThai", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;      // kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@matt", SqlDbType.Char).Value = maTT.Text;
            cmd.Parameters.Add("@tentt", SqlDbType.NVarChar).Value = TT.Text;
            cmd.ExecuteNonQuery();
            frmtrangthai_Load(sender, e);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("CapNhatTrangThai", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;      // kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@matt", SqlDbType.Char).Value = maTT.Text;
            cmd.Parameters.Add("@tentt", SqlDbType.NVarChar).Value = TT.Text;
            cmd.ExecuteNonQuery();
            frmtrangthai_Load(sender, e);
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("XoaTrangThai", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@matt", SqlDbType.Char).Value = maTT.Text;
            cmd.ExecuteNonQuery();
            frmtrangthai_Load(sender, e);
        }


        int dong;
        private void dt_trangthai_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            maTT.Text = dt_trangthai.Rows[dong].Cells["Ma_TT"].Value.ToString();
            TT.Text = dt_trangthai.Rows[dong].Cells["Ten_TT"].Value.ToString();
        }
    }
}
